package tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.rmi.RemoteException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.DocumentResult;
import org.dom4j.io.DocumentSource;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import ImlpToA.shareClassService;

public class ParseXml {

	public ParseXml() {
		// TODO Auto-generated constructor stub
	}
	

	public void saveXmlFile(File file){
		String name=file.getName();
	
		try {
			InputStream is=new FileInputStream(file);
			OutputStream os=new FileOutputStream(new File("XmlFile/"+name));
			String line;
			BufferedReader reader=new BufferedReader(new InputStreamReader(is));
			PrintWriter writer=new PrintWriter(new OutputStreamWriter(os));
			line=reader.readLine();
			while(line!=null){
				writer.println(line);
				line=reader.readLine();
			}
			writer.flush();
			is.close();
			os.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*try {
			FileInputStream fis=new FileInputStream(file);
			BufferedReader reader = new BufferedReader(new InputStreamReader(fis,"GBK"));
			FileOutputStream fos=new FileOutputStream(new File("XmlFile/"+name));
			
			byte[] b=new byte[1024];
			while(fis.read(b)!=-1){
				fos.write(b);
				fos.flush();
			}
			fos.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
	
	public void formatClassXml(File file,char depart){
		saveXmlFile(file);
		String resultFileName="";
		switch (depart) {
		case 'A':
			resultFileName="AClass.xml";
			break;
		case 'B':
			resultFileName="BClass.xml";
		case 'C':
			resultFileName="CClass.xml";

		default:
			break;
		}
		transform(file.getName(), "formatClass.xsl", resultFileName);
	}
	
	
	public void transform(String xmlFile,String xslFile,String resultFile){
		TransformerFactory factory=TransformerFactory.newInstance();
		try {
			File file=new File("XslFile/"+xslFile);
			Transformer transformer=factory.newTransformer(new StreamSource(file));
			SAXReader reader=new SAXReader();
			DocumentSource documentSource=new DocumentSource(reader.read("XmlFile/"+xmlFile));
			DocumentResult result=new DocumentResult();
			transformer.transform(documentSource,result);
			Document resultDocument=result.getDocument();
			toPrettyXML(resultDocument, resultFile);
		} catch (DocumentException | TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	public static void toPrettyXML(Document document,String resultFile){
		try {
			//Writer w=new FileWriter(resultFile);
			OutputStreamWriter writer=new OutputStreamWriter(new FileOutputStream("XmlFile/"+resultFile),"utf-8");
			OutputFormat opf=OutputFormat.createPrettyPrint();
			opf.setEncoding("utf-8");
		    XMLWriter xm=new XMLWriter(writer,opf);
		    xm.write(document);
		    
		    xm.close();
		    writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	public static void main(String[] args) {
		ParseXml transformXml=new ParseXml();
		try {
			shareClassService test=new shareClassService();
			test.shareClass(new File("test/test.xml"),'A');
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//transformXml.transform("test.xml", "formatClass.xsl", "result.xml");
		//transformXml.transform("test2.xml","studentToA.xsl","studentA.xml");
	}
}
